package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.system.model.SysTypeKey;
import com.ccp.dev.system.service.SysTypeKeyService;
import com.ccp.dev.core.basic.annotion.Action;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.ModelMap;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 系统分类键 信息操作处理控制器
 *
 * @author hw
 * @date 2019-09-09 13:31:25
 */
@Controller
@RequestMapping("/system/sysTypeKey")
public class SysTypeKeyController extends SysBaseController{
    @Resource
    private SysTypeKeyService sysTypeKeyService;

    /**
     * 功能描述: 跳转到首页
     *
     * @return: java.lang.String
     * @auther: hw
     * @date: 2019-09-09 13:31:25
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="跳转到系统分类键首页")
    public String list() {
        return "sysTypeKey/list";
    }

    /**
     * 功能描述: 跳转到新增页面
     *
     * @return: java.lang.String
     * @auther: hw
     * @date: 2019-09-09 13:31:25
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description="跳转到系统分类键新增页面")
    public String add(HttpServletRequest request,ModelMap modelMap) {
        String typeId = RequestUtil.getString(request,"typeId");
        modelMap.put("typeId",typeId);
        return "sysTypeKey/add";
    }

    /**
     * 功能描述:  数据保存通用方法
     *
     * @param: sysTypeKey 保存的对象
     * @return: java.lang.Object
     * @auther: hw
     * @date: 2019-09-09 13:31:25
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_SAVE)
    @ResponseBody
    @Action(description="保存系统分类键数据")
    public Object save(@RequestBody SysTypeKey sysTypeKey) {
        String id = sysTypeKey.getTypeId();

        int typeKeyLen = this.sysTypeKeyService.getByTypeKey(sysTypeKey).size();
        if(typeKeyLen > 0){
            return getResultDataError("operation.failure.notnull");
        }

        int result;
        if (StringUtil.isEmpty(id)) {
            //新增操作
            //新增数据前生成随机typeId
            sysTypeKey.setTypeId(UUIDUtils.getUUIDFor32());
            //新增时默认flag为自定义,排序为0
            sysTypeKey.setFlag(0);
            sysTypeKey.setSn(0);
            result = sysTypeKeyService.add(sysTypeKey);
        } else {
            //修改操作
            result = sysTypeKeyService.update(sysTypeKey);
        }
        return result > 0 ? getResultDataSuccess("save.success") : getResultDataError("save.failure");
    }

    /**
     * 功能描述: 根据主键ID删除数据的通用删除方法
     *
     * @param: id 参数名称必须是"typeId"
     * @return: java.lang.Object
     * @auther: hw
     * @date: 2019-09-09 13:31:25
     */
    @PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description="根据主键ID删除系统分类键数据")
    public Object delete(@RequestParam("id") String typeId) {
        if (StringUtil.isEmpty(typeId)) {
            return fail("delete.failure.message");
        }
        int result = sysTypeKeyService.delByIdArray(typeId.split(","));
        return result > 0 ? getResultDataSuccess("delete.success") : getResultDataError("delete.failure");
    }
    /**
     * 功能描述: 根据主键id获取一条数据的通用查询方法
     *
     * @param: id 参数对象必须是"typeId"
     * @return: java.lang.Object
     * @auther: hw
     * @date: 2019-09-09 13:31:25
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @ResponseBody
    @Action(description="根据主键id查询系统分类键的一条数据")
    public Object get(@RequestParam("id") String typeId) {
        SysTypeKey sysTypeKey = sysTypeKeyService.getOneById(typeId);
        if (null == sysTypeKey) {
            return getResultDataError("get.failure");
        }
        return getResultDataSuccess("get.success",sysTypeKey);
    }

    /**
     * 功能描述: 附带检索条件的分页查询
     *
     * @param: [request] http请求
     * @return: java.lang.Object
     * @auther: hw
     * @date: 2019-09-09 13:31:25
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description="附带检索条件的分页查询系统分类键")
    public Object list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, false);
        //分页数据
        List<SysTypeKey> pageDatas = sysTypeKeyService.queryList(queryFilter);
        return success(pageDatas, pageDatas.size());
    }

    /**
     * 根据id保存排序
     * @param ids
     * @return
     */
    @PostMapping("/saveSort")
    @ResponseBody
    @Action(description = "保存排序")
    public Object saveSort(@RequestParam("ids") String ids){
        if (StringUtils.isEmpty(ids)) {
            return success("没有数据，无法进行排序");
        }
        String[] typeIds = ids.split(",");
        int result = this.sysTypeKeyService.updateSequence(typeIds);
        return result > 0 ? getResultDataSuccess("operation.success") : getResultDataError("operation.failure");
    }


}
